package org.lyh.gift.service.impl;

import cn.hutool.core.lang.UUID;
import org.lyh.gift.client.LoginUserFeignClient;
import org.lyh.gift.domain.Employee;
import org.lyh.gift.domain.LoginUser;
import org.lyh.gift.domain.Tenant;
import org.lyh.gift.exception.BusinessException;
import org.lyh.gift.mapper.EmployeeMapper;
import org.lyh.gift.mapper.TenantMapper;
import org.lyh.gift.pojo.dto.TenantDTO;
import org.lyh.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.lyh.gift.util.AjaxResult;
import org.lyh.gift.util.BaiduAuditUtils;
import org.lyh.gift.util.BitStatesUtils;
import org.lyh.gift.util.encrypt.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyh
 * @since 2024-10-07
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private LoginUserFeignClient loginUserFeignClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Override
    public void entry(TenantDTO dto) {
//        //准备待审核的文本
//        ArrayList<String> strings = new ArrayList<>();
//        strings.add(dto.getLandline());
//        strings.add(dto.getEmail());
//        strings.add(dto.getPosition());
//        strings.add(dto.getAccount());
//        strings.add(dto.getPhone());
//        strings.add(dto.getShopName());
//        strings.add(dto.getCheckPass());
//        //准备待审核的图片路径
//        ArrayList<String> images = new ArrayList<>();
//        images.add(dto.getLogo());
//        //百度审核图片和文本
//        Map<String, Object> censor = BaiduAuditUtils.censor(strings,images);
//        Boolean success =(Boolean) censor.get("success");
//        if (!success){
//            throw new BusinessException("注册内容含违禁词！");
//        }

        //生成loginUser对象
        LoginUser loginUser = Bean2LoginUser(dto);
        //远程调用插入loginUser对象并返回loginUserid
        AjaxResult ajaxResult = loginUserFeignClient.addOrUpdate(loginUser);
        if (ajaxResult.isSuccess()){
            Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
            //生成employee对象
            Employee employee = Bean2Employee(loginUser);
            employee.setInputTime(new Date());
            employee.setLoginId(loginId);
            //给员工添加状态
            long l = BitStatesUtils.addState(BitStatesUtils.OP_REGISTED, BitStatesUtils.OP_ACTIVED);
            long state = BitStatesUtils.addState(l, BitStatesUtils.OP_AUTHED_PHONE);
            employee.setState(Integer.parseInt(Long.toString(state)));
            //将employee对象插入employee表并返回主键ID
            Integer adminId = employeeMapper.insert(employee);
            //生成tenant对象
            Tenant tenant = Bean2Tenant(dto);
            tenant.setAdminId(Long.parseLong(adminId.toString()));
            //插入tenant对象
            Integer tenantId = tenantMapper.insert(tenant);
            //补全employee并更新employee表
            employee.setTenantId(Long.parseLong(tenantId.toString()));
            employeeMapper.updateById(employee);
        }
    }

    private Tenant Bean2Tenant(TenantDTO dto) {
        Tenant tenant = new Tenant();
        tenant.setCompanyName(dto.getShopName());
        tenant.setAddress(dto.getPosition());
        tenant.setRegisterTime(new Date());
        tenant.setLogo(dto.getLogo());
        tenant.setState(0);
        return tenant;
    }

    private Employee Bean2Employee(LoginUser loginUser) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(loginUser,employee);
        return employee;
    }

    private LoginUser Bean2LoginUser(TenantDTO dto) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(dto.getAccount());
        loginUser.setPassword(MD5.getMD5(dto.getCheckPass()));
        loginUser.setTel(dto.getPhone());
        loginUser.setEmail(dto.getEmail());
        loginUser.setType(0);
        loginUser.setSalt(UUID.randomUUID().toString());
        return loginUser;
    }
}
